package com.higgins.lawapp.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import com.higgins.lawapp.client.application.dataModel.LawFirm;
import com.higgins.lawapp.server.dataItemModel.DataItem;
import com.higgins.lawapp.server.dataItemModel.DataItemContainer;

public class LawFirmDataService implements LawFirmDataServiceInterface	{
	PersistenceManager pm;
	
	public void openConnection()	{
		pm=PMF.get().getPersistenceManager();
	}

	public void closeConnection()	{
		pm.close();
	}

	@Override
	public List<String> retrieveRegions() {
		List<String> regions = new ArrayList<String>();
		regions.add("London");
		return regions;
	}

	public PersistenceManager getPersistenceManager()	{
		return pm;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<String> retrieveAreasOfLaw(String region) {
		List<String> listOfAreasOfLaw = new ArrayList<String>();
		Query query=pm.newQuery(DataItemContainer.class, "dataSource == dataSourceParam && dataLevel == dataLevelParam");
		query.declareParameters("String dataSourceParam, String dataLevelParam");
		List<DataItemContainer> results = (List<DataItemContainer>)query.execute("Law Careers", 
				"Law Careers");
		
		for (DataItemContainer container: results)
			for (DataItem dataItem:container.getDataItems())
				listOfAreasOfLaw.add(dataItem.getDataItemOne());
		return listOfAreasOfLaw;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> retrieveLawFirms(String region, String areaOfLaw) {
		List<String> listOfLawFirms = new ArrayList<String>();
		Query query=pm.newQuery(DataItemContainer.class, "dataSource == dataSourceParam && dataLevel == dataLevelParam && dataInstance == dataInstanceParam");
		query.declareParameters("String dataSourceParam, String dataLevelParam, String dataInstanceParam");
		List<DataItemContainer> results = (List<DataItemContainer>)query.execute("Law Careers", 
				"Area of Law", areaOfLaw);
		
		for (DataItemContainer container: results)
			for (DataItem dataItem:container.getDataItems())
				listOfLawFirms.add(dataItem.getDataItemTwo());
		return listOfLawFirms;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> retrieveLawFirmDetails(String region, String areaOfLaw,
			String lawFirm) {
		List<String> listOfLawFirmDetails = new ArrayList<String>();
		Query query=pm.newQuery(DataItemContainer.class, "dataSource == dataSourceParam && dataLevel == dataLevelParam && dataInstance == dataInstanceParam");
		query.declareParameters("String dataSourceParam, String dataLevelParam, String dataInstanceParam");
		List<DataItemContainer> results = (List<DataItemContainer>)query.execute("Law Careers", 
				"Law Firm", lawFirm);
		
		for (DataItemContainer container: results)
			for (DataItem dataItem:container.getDataItems())	{
				listOfLawFirmDetails.add(dataItem.getDataItemOne());
				listOfLawFirmDetails.add(dataItem.getDataItemTwo());
			}
		return listOfLawFirmDetails;
	}
	
	@SuppressWarnings("unchecked")
	public LawFirm retrieveLawFirm(String region, String areaOfLaw, String lawFirmName) {
		LawFirm lawFirm = new LawFirm();
		
		Query query=pm.newQuery(DataItemContainer.class, "dataSource == dataSourceParam && dataLevel == dataLevelParam && dataInstance == dataInstanceParam");
		query.declareParameters("String dataSourceParam, String dataLevelParam, String dataInstanceParam");
		List<DataItemContainer> results = (List<DataItemContainer>)query.execute("Law Careers", "Law Firm", lawFirmName);
		
		for (DataItemContainer container: results)	{
			lawFirm.setId(container.getId());
			lawFirm.setLawFirm(lawFirmName);
			lawFirm.setApply(container.getApply());
			lawFirm.setCoveringLetter(container.getCoveringLetter());
			for (DataItem dataItem:container.getDataItems())	{
				lawFirm.setDetailsId(container.getId());
				lawFirm.setAddress(dataItem.getDataItemOne());
				lawFirm.setContact(dataItem.getDataItemTwo());
			}
		}
		return lawFirm;
	}	

	@Override
	public List<LawFirm> retrieveLawFirmDetails(String region,
			String areaOfLaw) {
		List<String> lawFirms = retrieveLawFirms(region, areaOfLaw);
		List<LawFirm> lawFirmList = new ArrayList<LawFirm>();
		for (String lawFirmName:lawFirms)	{
			LawFirm lawFirm = retrieveLawFirm(region, areaOfLaw, lawFirmName);
			lawFirmList.add(lawFirm);
		}
		return lawFirmList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Boolean updateLawFirmPublished(Long id, String coveringLetter, Boolean published) {
		try	{
			DataItemContainer dataItem = (DataItemContainer) pm.getObjectById(DataItemContainer.class, id);
			
			dataItem.setApply(published);
			dataItem.setCoveringLetter(coveringLetter);
			pm.makePersistent(dataItem);
			
			Transaction transaction = null;
			transaction = pm.currentTransaction();
			transaction.begin();
			DataItemContainer dataItemTwo = (DataItemContainer) pm.getObjectById(DataItemContainer.class, id);
			System.out.println("Hello");
			transaction.commit();
		
		} catch (Exception e)	{		
			return false;
		}
		return true;
	}
	
	@SuppressWarnings("unchecked")
	List<LawFirm> retrieveLawFirmsToPublish(String coveringLetter) 	{
		List<LawFirm> lawFirms = new ArrayList<LawFirm>();
		List<DataItemContainer> results = null;
		try	{
			Query query=pm.newQuery(DataItemContainer.class, "dataSource == dataSourceParam && dataLevel == dataLevelParam && apply == dataSourceApply && coveringLetter == dataSourceCoveringLetter");
			query.declareParameters("String dataSourceParam, String dataLevelParam, Boolean dataSourceApply, String dataSourceCoveringLetter");
			List<Object> parameters = new ArrayList<Object>();
			parameters.add("Law Careers");
			parameters.add("Law Firm");
			parameters.add(new Boolean (true));
			parameters.add(coveringLetter);
			results = (List<DataItemContainer>)query.executeWithArray(parameters.toArray());
			
			for(DataItemContainer item:results)	{
				LawFirm lawFirm = new LawFirm();
				lawFirm.setLawFirm(item.getDataInstance());
				lawFirm.setCoveringLetter(coveringLetter);
				lawFirm.setApply(true);
				
				List<DataItem> dataItems = item.getDataItems();
				DataItem dataItem = dataItems.get(0);
				String address = dataItem.getDataItemOne();
				String contact = dataItem.getDataItemTwo();
				lawFirm.setAddress(address);
				lawFirm.setContact(contact);
				lawFirms.add(lawFirm);
			}
		} catch (Exception e)	{
			return null;
		}
		return lawFirms;
	}
	
}
